package controleurs;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import modele.CollectionBD;
import modele.ModeleP;
import vues.MainFrame;

public class BDExitControler extends WindowAdapter {
	private ModeleP myMod;
	private MainFrame myFrame;
	public BDExitControler(MainFrame f, ModeleP myMod){
		this.myFrame=f;
		this.myMod=myMod;
	}

	public void windowClosing(WindowEvent e){
		if((myMod.getCollec().getCheminPere()==null)){
			int answer=JOptionPane.showConfirmDialog(myFrame.f, "Sauvegarder la BDTheque ?",
					"Confirmation",
					JOptionPane.YES_OPTION,
					JOptionPane.QUESTION_MESSAGE);
			if(answer==JOptionPane.YES_OPTION){
				int returnVal = myFrame.fc.showSaveDialog(myFrame.f);
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	            	CollectionBD.sauvegarder(myMod.getCollec(), myFrame.fc.getSelectedFile().getAbsolutePath());
	            	System.exit(0);
	            }
			} else
			System.exit(0);
		} else {
			myMod.getCollec();
			CollectionBD.sauvegarder(myMod.getCollec(), myMod.getCollec().getCheminPere());
			System.exit(0);
		}
	}

}
